﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Common;
using Domain.Officers;
using Service;

namespace UIComponentSprite.Building.Talents
{
    public partial class SelectVisiteOfficerListForm : Form
    {
        public Domain.Building CurrentBuilding;
        public int TargetOfficerId;
        public SelectVisiteOfficerListForm(short buildId,int targetOfficerId)
        {
            this.TargetOfficerId = targetOfficerId;
            this.CurrentBuilding = Domain.Building.Buildings[buildId];
            InitializeComponent();
            this.Text += "--选择登庸武将";
        }

        private void DispatchOfficerList_Load(object sender, EventArgs e)
        {
            CreateCustomColumns();
            DataBind();
            SetGridViewColumnStyle();

        }

        private void CreateCustomColumns()
        {
            DataGridViewCheckBoxColumn column1 = new DataGridViewCheckBoxColumn();
            column1.DisplayIndex = 0;
            column1.Name = "Select";
            column1.DataPropertyName = "Select";
            column1.TrueValue = true;
            column1.FalseValue = false;
            dataGridView1.Columns.Add(column1);
            DataGridViewImageColumn column2 = new DataGridViewImageColumn();
            column2.DisplayIndex = 1;
            column2.Name = "Pic";
            column2.DataPropertyName = "Pic";
            column2.ImageLayout = DataGridViewImageCellLayout.Zoom;
            dataGridView1.Columns.Add(column2);
        }
        public void DataBind()
        {
            DataTable tb = CreateTable();
            List<Officer> list = CurrentBuilding.OwnedOfficers.Where(x => x.InBuilding!=null&&x.InBuilding.Id == this.CurrentBuilding.Id).ToList();
            foreach (var officer in list)
            {
                DataRow dr = tb.NewRow();
                dr["ID"] = officer.Id;
                dr["PicNumber"] = officer.PicNumber;
                dr["姓名"] = officer.FirstName + officer.Name;
                dr["工作"] = EnumUtil.Getdescription(officer.WorkType);
                dr["政治"] = officer.Politics;
                dr["智力"] = officer.Intelligence;
                dr["统帅"] = officer.Commander;
                dr["武力"] = officer.Strength;
                dr["魅力"] = officer.Glamour;
                dr["名望"] = officer.Reputation;
                tb.Rows.Add(dr);
            }
            BindingSource bs = new BindingSource();
            bs.DataSource = tb;
            dataGridView1.DataSource = bs;

        }

        private void SetGridViewColumnStyle()
        {
            this.dataGridView1.Columns["Select"].HeaderText = " ";
            this.dataGridView1.Columns["Select"].Width = 30;
            this.dataGridView1.Columns["Pic"].HeaderText = " ";
            this.dataGridView1.Columns["Pic"].Width = 31;
            this.dataGridView1.Columns["ID"].Visible = false;
            this.dataGridView1.Columns["PicNumber"].Visible = false;
            this.dataGridView1.Columns["姓名"].Width = 60;
            this.dataGridView1.Columns["工作"].Width = 60;
            this.dataGridView1.Columns["政治"].Width = 60;
            this.dataGridView1.Columns["智力"].Width = 60;
            this.dataGridView1.Columns["统帅"].Width = 60;
            this.dataGridView1.Columns["武力"].Width = 60;
            this.dataGridView1.Columns["魅力"].Width = 60;
            this.dataGridView1.Columns["名望"].Width = 60;
        }
        public DataTable CreateTable()
        {
            DataTable tb = new DataTable();
            tb.Columns.Add("ID", Type.GetType("System.Int32"));
            tb.Columns.Add("PicNumber", Type.GetType("System.String"));
            tb.Columns.Add("姓名", Type.GetType("System.String"));
            tb.Columns.Add("工作", Type.GetType("System.String"));
            tb.Columns.Add("政治", Type.GetType("System.Int32"));
            tb.Columns.Add("智力", Type.GetType("System.Int32"));
            tb.Columns.Add("统帅", Type.GetType("System.Int32"));
            tb.Columns.Add("武力", Type.GetType("System.Int32"));
            tb.Columns.Add("魅力", Type.GetType("System.Int32"));
            tb.Columns.Add("名望", Type.GetType("System.Int32"));
            return tb;
        }
        /// <summary>
        /// 单元格点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex != -1)
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    dataGridView1.Rows[i].Cells["Select"].Value = false;
                }
                //获取控件的值
                if ((bool)this.dataGridView1.Rows[e.RowIndex].Cells["Select"].EditedFormattedValue == false)
                {
                    this.dataGridView1.Rows[e.RowIndex].Cells["Select"].Value = true;
                }
                else
                {
                    this.dataGridView1.Rows[e.RowIndex].Cells["Select"].Value = false;
                }
            }
        }

        private void btnSure_Click(object sender, EventArgs e)
        {
            //循环dataGridView
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                var selected = (bool)dataGridView1.Rows[i].Cells["Select"].Value;
                if (selected)
                {
                    int officerId = (int)dataGridView1.Rows[i].Cells["ID"].Value;
                    TalentsService.DispatchVisiteJob(officerId, TargetOfficerId);
                    DataBind();
                }

            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e == null || e.Value == null || !(sender is DataGridView))
                return;
        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                dataGridView1.Rows[i].Cells["Select"].Value = false;
                string pic = (string)dataGridView1.Rows[i].Cells["PicNumber"].Value;
                dataGridView1.Rows[i].Cells["Pic"].Value = Image.FromFile(@"" + System.IO.Directory.GetCurrentDirectory() + @"\Images\person\" + pic + "s.jpg");
            }
        }
        private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            
        }

    }
}
